package com.thunder.soft.vm;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.thunder.soft.bean.BannerBean;
import com.thunder.soft.bean.HomeDataBean;
import com.thunder.soft.common.base.BaseBean;
import com.thunder.soft.common.base.BaseViewModel;
import com.thunder.soft.common.net.HttpClient;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.RequestBody;
import okhttp3.Response;

public class HomeViewModel extends BaseViewModel {

    private int mPage = 0;
    private static final String TAG = HomeViewModel.class.getName();

    private MutableLiveData<List<BannerBean>> bannerLiveData = new MutableLiveData<>();
    private MutableLiveData<HomeDataBean> homeDataLiveData = new MutableLiveData<>();

    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        super.onCreate(owner);
        getBanner();
    }

    public void getBanner() {
        /**
         * 网络请求banner数据
         */
        HttpClient.getInstance().request("banner/json", "GET", null, new Callback() {

            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                getErrorMsg().postValue(e.getMessage());
                Log.d(TAG, "error");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Gson gson = new Gson();

                String string = response.body().string();
                Log.e(TAG, string);
                BaseBean<List<BannerBean>> bannerBean = gson.fromJson(string,
                        new TypeToken<BaseBean<List<BannerBean>>>() {
                        }.getType());

                if (bannerBean.getErrorCode() == 0) {
                    bannerLiveData.postValue(bannerBean.getData());
                    Log.d(TAG, "success");
                } else {
                    getErrorMsg().postValue(bannerBean.getErrorMsg());
                }
            }
        });
    }

    /**
     * 网络请求article数据
     */
    public void getArticle() {
        HttpClient.getInstance().request("article/list/" + mPage + "/json", "GET", null, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                getErrorMsg().postValue(e.getMessage());
                Log.d(TAG, "error");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Gson gson = new Gson();
                String string = response.body().string();
                Log.e(TAG, string);

                BaseBean<HomeDataBean> homeDataBean = gson.fromJson(string,
                        new TypeToken<BaseBean<HomeDataBean>>() {
                        }.getType());

                if (homeDataBean.getErrorCode() == 0) { //success
                    homeDataLiveData.postValue(homeDataBean.getData());
                } else {
                    getErrorMsg().postValue(homeDataBean.getErrorMsg());
                }
            }
        });
    }

    public void addCollect(int id) {

        RequestBody requestBody = new FormBody.Builder()
                .build();

        HttpClient.getInstance().request("lg/collect/" + id + "/json", "POST", requestBody, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                getErrorMsg().postValue(e.getMessage());
                Log.d(TAG, "error");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {

                Gson gson = new Gson();
                String string = response.body().string();
                Log.e(TAG, string);

            }
        });
    }

    public void cancelCollect(int id) {

        RequestBody requestBody = new FormBody.Builder()
                .build();

        HttpClient.getInstance().request("lg/uncollect_originId/" + id + "/json", "POST", requestBody, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                getErrorMsg().postValue(e.getMessage());
                Log.d(TAG, "error");
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                Gson gson = new Gson();
                String string = response.body().string();
                Log.e(TAG, string);
            }
        });
    }

    public void refreshData(Boolean refresh) {
        if (refresh) {
            mPage = 0;
        } else {
            mPage++;
        }
        getArticle();
    }

    public void onBackPressed() {
        getFinishActivity().postValue(1);
    }

    public MutableLiveData<List<BannerBean>> getBannerLiveData() {
        return bannerLiveData;
    }

    public MutableLiveData<HomeDataBean> getHomeDataLiveData() {
        return homeDataLiveData;
    }
}